﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using HengTong.Model._Admin;
using HengTong.Model.Db;
using HengTong.Model.Db.Hr;
using HengTong.Model.Extension;
using WalkingTec.Mvvm.Core;
using Microsoft.EntityFrameworkCore;

namespace HengTong.ViewModel._Admin.FrameworkUserVMs
{
    public partial class FrameworkUserVM : BaseCRUDVM<FrameworkUser>
    {
        [Display(Name = "_Model.FrameworkRole")]
        public List<string> SelectedFrameworkUserRolesIDs { get; set; }

        [Display(Name = "_Model.FrameworkGroup")]
        public List<string> SelectedFrameworkUserGroupsIDs { get; set; }

        public FrameworkUserVM()
        {
        }

        protected override void InitVM()
        {
            SelectedFrameworkUserRolesIDs = DC.Set<FrameworkUserRole>().Where(x => x.UserCode == Entity.ITCode)
                .Select(x => x.RoleCode).ToList();
            SelectedFrameworkUserGroupsIDs = DC.Set<FrameworkUserGroup>().Where(x => x.UserCode == Entity.ITCode)
                .Select(x => x.GroupCode).ToList();
        }

        public override DuplicatedInfo<FrameworkUser> SetDuplicatedCheck()
        {
            var rv = CreateFieldsInfo(SimpleField(x => x.ITCode));
            return rv;
        }

        public override async Task DoAddAsync()
        {
            Entity.CreateBy = Wtm.LoginUserInfo?.ITCode;
            Entity.UpdateBy = Wtm.LoginUserInfo?.ITCode;
            Entity.CreateTime = DateTime.Now;
            Entity.UpdateTime = DateTime.Now;
            await base.DoAddAsync();
            Wtm.RemoveAllAccountsCache();
        }

        public override async Task DoEditAsync(bool updateAllFields = false)
        {
            // 开启事务
            var transaction = await DC.Database.BeginTransactionAsync();
            try
            {
                var person = await DC.Set<SysPerson>()
                    .AsNoTracking()
                    .FirstOrDefaultAsync(x => x.StaffNo == Entity.ITCode);
                // 判断 当前用户是否存在人员档案
                if (person != null)
                {
                    //判断 手机号 是否一致，不一致则更新
                    if (person.Phone != Entity.CellPhone)
                    {
                        await DC.Set<SysPerson>().Where(w => w.StaffNo == Entity.ITCode)
                            .ExecuteUpdateAsync(u => u.SetProperty(x => x.Phone, Entity.CellPhone)
                            );
                        await DC.SaveChangesAsync();
                    }
                }

                Entity.UpdateTime = DateTime.Now;
                Entity.UpdateBy = Wtm.LoginUserInfo?.ITCode;
                await base.DoEditAsync(updateAllFields);
                await Wtm.RemoveUserCache(Entity.ITCode);
                // 提交事务
                await transaction.CommitAsync();
            }
            catch (Exception e)
            {
                // 回滚事务
                await transaction.RollbackAsync();
                MSD.AddModelError(" ", $"编辑操作异常: {e.Message}");
            }
            finally
            {
                // 释放事务
                await transaction.DisposeAsync();
            }
        }
        public override void DoDelete()
        {
            base.DoDelete();
            Wtm.RemoveAllAccountsCache();
        }
    }
}